package cn.lger.controller;

import cn.lger.pojo.Administrator;
import cn.lger.pojo.User;
import cn.lger.service.AdminService;
import cn.lger.service.UserService;
import cn.lger.util.StringEmptyUtil;
import cn.lger.util.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Code that Changed the World
 *
 * @author Pro
 * @date 2018-06-14.
 */
@Controller
@Api(tags = "登录控制器")
public class LoginController {

    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;


    @GetMapping("login")
    @ApiOperation(httpMethod = "GET", value = "返回登录页面")
    public String loginView(){
        return "login";
    }

    @GetMapping("register")
    @ApiOperation(httpMethod = "GET", value = "返回注册页面")
    public String registerView(){
        return "register";
    }

    @PostMapping("login")
    @ApiOperation(httpMethod = "POST", value = "登录操作")
    public String login(@RequestParam @ApiParam(value = "用户名", required = true) String username,
                        @RequestParam @ApiParam(value = "密码", required = true) String password,
                        @ApiIgnore HttpSession session){
        User temp = userService.findOneByUsernameAndPassword(username, password);
        if (temp != null){
            session.setAttribute("user", temp);
            String url = (String) session.getAttribute("url");
            if (StringEmptyUtil.notEmpty(url)) {
                return "redirect:"+url;
            }else {
                return "redirect:/";
            }
        }
        return "login";
    }

    @PostMapping("register")
    @ApiOperation(httpMethod = "POST", value = "注册操作")
    @ApiResponse(code = 200, message = "注册成功返回登录页面")
    public String register(@Validated @ApiParam("用户实体类") User user, @ApiIgnore HttpServletRequest request){
        try {
            if (!ValidateUtil.isValidEmail(user.getEmail())){
                request.setAttribute("msg", "邮箱格式错误");
                return "login";
            }
            if (!StringEmptyUtil.notEmpty(user.getUsername()) && !StringEmptyUtil.notEmpty(user.getPassword())){
                request.setAttribute("msg", "用户名密码不为空");
                return "login";
            }
            if (userService.insertOne(user) != 0){
                return "login";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        request.setAttribute("user", user);
        return "register";
    }

    @GetMapping("adminLogin")
    @ApiOperation("返回管理员登录页面")
    public String adminLogin() {
        return "adminLogin";
    }

    @PostMapping("adminLogin")
    @ApiOperation(httpMethod = "POST", value = "管理员登录操作")
    public String adminLogin(@RequestParam @ApiParam(value = "管理员用户名", required = true) String username,
                             @RequestParam @ApiParam(value = "管理员密码", required = true) String password,
                             @ApiIgnore HttpSession session){
        Administrator temp = adminService.findOneByUsernameAndPassword(username, password);
        if (temp != null){
            session.setAttribute("admin", temp);
            String url = (String) session.getAttribute("url");
            if (StringEmptyUtil.notEmpty(url)) {
                return "redirect:"+url;
            }else {
                return "admin";
            }
        }
        return "adminLogin";
    }

    @GetMapping("logout")
    @ApiOperation(value = "用户登出操作、成功后跳转主页", httpMethod = "GET")
    public String logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
        return "index";
    }

    @GetMapping("adminLogout")
    @ApiOperation(value = "管理员登出操作、成功后跳转主页", httpMethod = "GET")
    public String adminLogout(@ApiIgnore HttpSession session){
        session.removeAttribute("admin");
        return "redirect:admin";
    }

}
